//
//  UIImage+EJUExtend.m
//  ECM
//
//  Created by 孙文强 on 16/9/17.
//  Copyright © 2016年 Happy Day. All rights reserved.
//

#import "UIImage+EJUExtend.h"

@implementation UIImage (EJUExtend)

+ (UIImage *)createImageWithColor:(UIColor*)color {
    
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return theImage;
}

- (UIImage *)resizableImage {
    CGSize imageSize = self.size;
    return [self resizableImageWithCapInsets:UIEdgeInsetsMake(imageSize.height/2+10, imageSize.width/2, imageSize.height/2-10, imageSize.width/2)];
}

-(NSData *)dataWithCompressQuality:(CGFloat)compressionQuality
{
    NSData *imgData = UIImageJPEGRepresentation(self, compressionQuality);
    return imgData;
}

-(NSData *)dataWithCompressLength:(CGFloat)length
{
    CGFloat quality = 1;
    NSData * imgData = UIImageJPEGRepresentation(self, quality);
    while (imgData.length > length) {
        quality -= 0.1;
        imgData = UIImageJPEGRepresentation(self,quality);
        if (quality <0.2)
            break;
    };
    return imgData;
}

- (UIImage *)imageScaledToHeight:(CGFloat)height
{
    if (self.size.height < height)
        return self;
    CGSize newSize;
    newSize.height = height;
    newSize.width = self.size.width * height / self.size.height;
    
    UIGraphicsBeginImageContextWithOptions(newSize, YES, 0);
    [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

- (UIImage *)imageScaledToWidth:(CGFloat)width
{
    if (self.size.width < width)
        return self;
    CGSize newSize;
    newSize.width = width;
    newSize.height = self.size.height * width / self.size.width;
    
    UIGraphicsBeginImageContextWithOptions(newSize, YES, 0);
    [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

- (UIImage *)cutImageFromCenter:(CGSize)size
{
    CGRect rect;
    float x,y,w,h;
    if (self.size.height <= size.height)
        x = 0,h = self.size.height;
    else
        x = self.size.height/2 - size.height/2,h = size.height;
    
    if (self.size.width <= size.width)
        y = 0,w = self.size.width;
    else
        y = self.size.width/2 - size.width/2, w = size.width;
    
    rect = CGRectMake(x, y, w, h);
    return [UIImage imageWithCGImage:CGImageCreateWithImageInRect([self CGImage], rect)];
}

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 44.f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

//不要边框，只把图片变成圆形
+ (UIImage *)ellipseImage:(UIImage *)image withInset:(CGFloat)inset
{
    return [self ellipseImage:image withInset:inset withBorderWidth:0 withBorderColor:[UIColor clearColor]];
}

+ (UIImage *)ellipseImage: (UIImage *) image withInset:(CGFloat)inset withBorderWidth:(CGFloat)width withBorderColor:(UIColor*)color
{
    UIGraphicsBeginImageContext(image.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f , image.size.height - inset * 2.0f);
    
    CGContextAddEllipseInRect(context, rect);
    CGContextClip(context);
    [image drawInRect:rect];
    
    if (width > 0) {
        CGContextSetStrokeColorWithColor(context, color.CGColor);
        CGContextSetLineCap(context,kCGLineCapButt);
        CGContextSetLineWidth(context, width);
        CGContextAddEllipseInRect(context, CGRectMake(inset + width/2, inset +  width/2, image.size.width - width- inset * 2.0f, image.size.height - width - inset * 2.0f));//在这个框中画圆
        
        CGContextStrokePath(context);
    }
    
    
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newimg;
}

- (UIImage *)blurredImageWithRadius:(CGFloat)radius iterations:(NSUInteger)iterations tintColor:(UIColor *)tintColor
{
    //    //image must be nonzero size
    //    if (floorf(self.size.width) * floorf(self.size.height) <= 0.0f) return self;
    //
    //    //boxsize must be an odd integer
    //    uint32_t boxSize = (uint32_t)(radius * self.scale);
    //    if (boxSize % 2 == 0) boxSize ++;
    //
    //    //create image buffers
    //    CGImageRef imageRef = self.CGImage;
    //    vImage_Buffer buffer1, buffer2;
    //    buffer1.width = buffer2.width = CGImageGetWidth(imageRef);
    //    buffer1.height = buffer2.height = CGImageGetHeight(imageRef);
    //    buffer1.rowBytes = buffer2.rowBytes = CGImageGetBytesPerRow(imageRef);
    //    size_t bytes = buffer1.rowBytes * buffer1.height;
    //    buffer1.data = malloc(bytes);
    //    buffer2.data = malloc(bytes);
    //
    //    //create temp buffer
    //    void *tempBuffer = malloc((size_t)vImageBoxConvolve_ARGB8888(&buffer1, &buffer2, NULL, 0, 0, boxSize, boxSize,
    //                                                                 NULL, kvImageEdgeExtend + kvImageGetTempBufferSize));
    //
    //    //copy image data
    //    CFDataRef dataSource = CGDataProviderCopyData(CGImageGetDataProvider(imageRef));
    //    memcpy(buffer1.data, CFDataGetBytePtr(dataSource), bytes);
    //    CFRelease(dataSource);
    //
    //    for (NSUInteger i = 0; i < iterations; i++)
    //    {
    //        //perform blur
    //        vImageBoxConvolve_ARGB8888(&buffer1, &buffer2, tempBuffer, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    //
    //        //swap buffers
    //        void *temp = buffer1.data;
    //        buffer1.data = buffer2.data;
    //        buffer2.data = temp;
    //    }
    //
    //    //free buffers
    //    free(buffer2.data);
    //    free(tempBuffer);
    //
    //    //create image context from buffer
    //    CGContextRef ctx = CGBitmapContextCreate(buffer1.data, buffer1.width, buffer1.height,
    //                                             8, buffer1.rowBytes, CGImageGetColorSpace(imageRef),
    //                                             CGImageGetBitmapInfo(imageRef));
    //
    //    //apply tint
    //    if (tintColor && CGColorGetAlpha(tintColor.CGColor) > 0.0f)
    //    {
    //        CGContextSetFillColorWithColor(ctx, [tintColor colorWithAlphaComponent:0.25].CGColor);
    //        CGContextSetBlendMode(ctx, kCGBlendModePlusLighter);
    //        CGContextFillRect(ctx, CGRectMake(0, 0, buffer1.width, buffer1.height));
    //    }
    //
    //    //create image from context
    //    imageRef = CGBitmapContextCreateImage(ctx);
    //    UIImage *image = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
    //    CGImageRelease(imageRef);
    //    CGContextRelease(ctx);
    //    free(buffer1.data);
    //    return image;
    return self;
}

////  获取PNG图片的大小
//+(CGSize)getPNGImageSizeWithRequest:(NSMutableURLRequest*)request
//{
//    [request setValue:@"bytes=16-23" forHTTPHeaderField:@"Range"];
//    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//    if(data.length == 8)
//    {
//        int w1 = 0, w2 = 0, w3 = 0, w4 = 0;
//        [data getBytes:&w1 range:NSMakeRange(0, 1)];
//        [data getBytes:&w2 range:NSMakeRange(1, 1)];
//        [data getBytes:&w3 range:NSMakeRange(2, 1)];
//        [data getBytes:&w4 range:NSMakeRange(3, 1)];
//        int w = (w1 << 24) + (w2 << 16) + (w3 << 8) + w4;
//        int h1 = 0, h2 = 0, h3 = 0, h4 = 0;
//        [data getBytes:&h1 range:NSMakeRange(4, 1)];
//        [data getBytes:&h2 range:NSMakeRange(5, 1)];
//        [data getBytes:&h3 range:NSMakeRange(6, 1)];
//        [data getBytes:&h4 range:NSMakeRange(7, 1)];
//        int h = (h1 << 24) + (h2 << 16) + (h3 << 8) + h4;
//        return CGSizeMake(w, h);
//    }
//    return CGSizeZero;
//}

@end
